<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 分类控制器
 * @author  Karson
 * @name    category.php
 * @since   2013-8-10 20:17:14
 */
class Category_Controller extends Controller {

    function _init() {
        check_level();
        $this->category->load_user_category();
    }

    function index() {
        render("category/index");
    }

    function add($type = 'expense') {
        if ($this->form->is_submit()) {
            $father_id = $this->input->post("father_id");
            $name = $this->input->post("name");
            $one = $this->db->one("SELECT weigh FROM {pre}category WHERE user_id='{$this->auth->user_id}' AND type='{$type}' ORDER BY weigh DESC");
            $weigh = $one ? $one['weigh'] + 1 : 1;
            $value = array(
                'uuid' => get_uuid(), 'father_id' => $father_id, 'user_id' => $this->auth->user_id,
                'name' => $name, 'createtime' => TIME,
                'type' => $type,
                'weigh' => $weigh,
                'modifytime' => TIME, 'state' => 1
            );
            $value['id'] = $this->db->insert("{pre}category", $value);
            $result = array('info' => '提交成功！', 'status' => 'y', 'data' => $value);
            echo json_encode($result);
            exit;
        }
        $this->view->assign("type", $type);
        render("category/add");
    }

    function edit($type = 'expense') {
        if ($this->form->is_submit()) {
            $id = $this->input->post("id");
            $father_id = $this->input->post("father_id");
            $name = $this->input->post("name");
            $value = array(
                'name' => $name,
                'modifytime' => TIME
            );
            $value['id'] = $this->db->update("{pre}category", $value, array('id' => $id));
            $result = array('info' => '提交成功！', 'status' => 'y', 'data' => $value);
            echo json_encode($result);
            exit;
        }
        $this->view->assign("type", $type);
        render("category/add");
    }

    function del($id) {
        $category = get_category($id);
        if ($category) {
            if (!$category['system']) {
                $ids = array($id);
                //如果删除的是父分类,则再删除子分类所有数据
                if ($category['father_id'] == 0) {
                    foreach ($this->category->usercategory as $m => $n) {
                        if ($n['father_id'] > 0 && $n['father_id'] == $id) {
                            $ids[] = $n['id'];
                        }
                    }
                }
                //将流水记录分类置为0
                $this->db->query("UPDATE {pre}bill SET category_id=0 WHERE category_id IN (" . implode(',', $ids) . ") AND user_id='{$this->auth->user_id}'");
                //删除分类
                $this->db->query("DELETE FROM {pre}category WHERE id IN (" . implode(',', $ids) . ") AND user_id='{$this->auth->user_id}'");
                $code = 0;
                $content = '删除成功';
            } else {
                $code = -2;
                $content = "系统分类不能删除和修改";
            }
        } else {
            $code = -1;
            $content = '分类未找到';
        }
        json_output($code, $content);
    }

    function light($id, $state) {
        $category = get_category($id);
        if ($category) {
            //将流水记录分类置为0
            $this->db->update("{pre}category", array("state" => $state), array("id" => $id, "user_id" => $this->auth->user_id));
            if ($category['father_id'] == 0) {
                $this->db->update("{pre}category", array("state" => $state), array("father_id" => $id, "user_id" => $this->auth->user_id));
            } else {
                if ($state == 1) {
                    $this->db->update("{pre}category", array("state" => $state), array("id" => $category['father_id'], "user_id" => $this->auth->user_id));
                }
            }
            $code = 0;
            $content = '操作成功';
        } else {
            $code = -1;
            $content = '分类未找到';
        }
        json_output($code, $content);
    }

}